package com.zhongxi.guoyuan.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.okhttp.Request;
import com.zhongxi.guoyuan.R;

import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.http.OkHttpClientManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class BaseFragment extends Fragment {

    protected TextView tvTitle;
    protected ImageView ivBack;
    protected Context context;

    protected int pageNum = 1;
    private ProgressDialog progressDialog;

    class StringCallback extends OkHttpClientManager.ResultCallback<String> {
        @Override
        public void onResponse(String response) {
            Log.i("base", response);
                try {
                    JSONObject obj = new JSONObject(response);
                    int code = obj.getInt("code");
                    if(code == 1){
                        processSuccessResult(response);
                    }else if(code == 7002){
                        processOldUser(response);
                    }else{
                        ProcessFailResult(response);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }finally {
                    hideProgressDialog();
                }
            }
        @Override
        public void onError(Request request, Exception e) {
            processFailRequest();
        }
    }

    private void processFailRequest() {

    }

    protected void processOldUser(String response) {

    }

    /**
     * 请求失败
     */
    protected void ProcessFailResult(String response) {

    }

    /**
     * 请求成功
     */
    protected void processSuccessResult(String response) {

    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity();
        initView();
        initData();
        initEvent();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    protected void initTitle(){
        tvTitle = (TextView) getView().findViewById(R.id.tvTitle);
        ivBack = (ImageView) getView().findViewById(R.id.ivBack);
    }

    protected void initEvent() {

    }

    protected void initData() {

    }

    protected void initView(){

    }

    /**
     * 获取网络请求 map -- 传递参数
     * @param
     * @param getStr
     */
    public void getServer(String url, Map<String, String> map, String getStr, boolean isUser, StringCallback callback, String type){
        try {
            OkHttpClientManager okHttp = OkHttpClientManager.getInstance();
            if (map == null) {
                map = new HashMap<String, String>();
            }
            if(callback == null){
                callback = new StringCallback();
            }
            String baseUrl = url + MyApplication.getInstance().getCheck(isUser);
            Log.i("base", baseUrl);
            if ("get".equals(type)) {
                okHttp.getAsyn(baseUrl+getStr, callback);
            } else {
                okHttp.postAsyn(baseUrl, callback, map);
            }
            showProgressDialog(type);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 展示提交或者加载数据的进度框
     * @param method
     */
    private void showProgressDialog(String method) {
        if(progressDialog == null)
            progressDialog = new ProgressDialog(getActivity(), ProgressDialog.THEME_HOLO_LIGHT);
        progressDialog.setCancelable(true);
        String progressDialogMessage = "";
        if(!progressDialog.isShowing()){
            if(method.equals("get"))
                progressDialogMessage = getResources().getString(R.string.get);
            else if(method.equals("upload"))
                progressDialogMessage = getResources().getString(R.string.upload);
            else
                progressDialogMessage = "正在请求数据……";
            progressDialog.setMessage(progressDialogMessage);
            progressDialog.show();
        }
    }

    /**
     * 隐藏对话框
     */
    private void hideProgressDialog() {
        if(progressDialog != null && progressDialog.isShowing()){
            progressDialog.dismiss();
            progressDialog.cancel();
        }
    }

    public void startAct(Class clazz){
        Intent intent = new Intent(context, clazz);
        context.startActivity(intent);
    }

    public void showShortToast(String message) {
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }
}
